package com.e3.basdoc.employeecls.client;

import java.util.ArrayList;
import java.util.List;

import com.e3.basdoc.employeecls.model.EmployeeClsModel;
import com.e3.template.tgf.TGFHelper;
import com.e3.web.widget.client.MyFormFieldFactory;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.RowNumberer;

public class EmployeeClsHelper extends TGFHelper<EmployeeClsModel>{
	
	public static EmployeeClsHelper INSTANCE = new EmployeeClsHelper();
	
	private String[] notNullFields = {EmployeeClsModel.CODE,EmployeeClsModel.NAME};

	public Field<?> createFormField(String name){
		Field<?> formField = null;
		String dispName = getFieldDisplayName(name);
		boolean isNotNull = isNotNullField(name);
		if(name.equals(EmployeeClsModel.ID)){
			formField = MyFormFieldFactory.createIdField(name);
		}		
		else if(name.equals(EmployeeClsModel.CODE)){
			formField = MyFormFieldFactory.createStringField(name,dispName,20,isNotNull);
		}
		else if(name.equals(EmployeeClsModel.NAME)){
			formField = MyFormFieldFactory.createStringField(name,dispName,50,isNotNull);
		}
		else if(name.equals(EmployeeClsModel.REMARK)){
			formField = MyFormFieldFactory.createTextAreaField(name, dispName,100,isNotNull);
		}
		
		return formField;
	}
	
	public List<ColumnConfig> getGridColumnConfigs(){
		List<ColumnConfig> configs = new ArrayList<ColumnConfig>();
		RowNumberer numberer = new RowNumberer();   
		configs.add(numberer);
		
		ColumnConfig column = new ColumnConfig();
		String name = EmployeeClsModel.CODE;
	    column.setId(name);   
	    column.setHeader(getFieldDisplayName(name));   
	    column.setWidth(200);   
	    configs.add(column);  
	    
	    column = new ColumnConfig();
		name = EmployeeClsModel.NAME;
	    column.setId(name);   
	    column.setHeader(getFieldDisplayName(name));   
	    column.setWidth(200);   
	    configs.add(column);  
    
	    column = new ColumnConfig();
		name = EmployeeClsModel.REMARK;
	    column.setId(name);   
	    column.setHeader(getFieldDisplayName(name));   
	    column.setWidth(200);   
	    configs.add(column);
	    
	    return configs;
	}
	
	public String getFieldDisplayName(String code) {
		return EmployeeClsFieldsConstant.INSTANCE.getString(code);
	}
	
	public EmployeeClsModel createRootModel(){
		EmployeeClsModel model = new EmployeeClsModel();
		model.setId(0L);
		model.setCode("0");
		model.setHasChildren(true);
		return model;
	}
	
	public boolean isRootModel(EmployeeClsModel model){
		if(model==null){
			return false;
		}
		return "0".equals(model.getCode());
	}
	
	/**
	 * 是否是最高层。
	 * 
	 * @param model
	 * @return
	 */
	public boolean isTopModel(EmployeeClsModel model){
		if(model==null){
			return false;
		}
		return "0".equals(model.getParentId());
	}
	
	public void setModelRelations(EmployeeClsModel pModel,List<EmployeeClsModel> children){
		//pModule.setSubModules(children);
		for (EmployeeClsModel model : children) {
			model.setParentModel(pModel);
		}
	}

	@Override
	public String[] getNotNullFieldNames() {
		return notNullFields;
	}

	@Override
	public EmployeeClsModel createModelInstance() {
		return new EmployeeClsModel();
	}

	@Override
	public String getCodeFieldName() {
		return EmployeeClsModel.CODE;
	}
}
